package com.miao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.miao.base.model.PageParams;
import com.miao.base.model.PageResult;
import com.miao.base.model.RestResponse;
import com.miao.dto.MediaConditionDto;
import com.miao.dto.MediaParamsDto;
import com.miao.mapper.MediaFilesMapper;
import com.miao.pojo.MediaFiles;
import com.miao.service.IMediaFilesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
public class BigFilesController {

    @Autowired
    private IMediaFilesService mediaFilesService;

    //检查文件是否存在
    @PostMapping("/upload/checkfile")
    public RestResponse<Boolean> checkFile(String fileMd5){
        return mediaFilesService.checkFile(fileMd5);
    }

    //检查分块是否存在
    @PostMapping("/upload/checkchunk")
    public RestResponse<Boolean> checkChunk(String fileMd5, int chunk){
        return mediaFilesService.checkchunk(fileMd5, chunk);
    }

    //上传分块
    @PostMapping("/upload/uploadchunk")
    public RestResponse<Boolean> uploadChunk(MultipartFile file, String fileMd5, int chunk) throws Exception {
        log.error("上传分块了！");
        File minio = File.createTempFile("minio", ".temp");
        file.transferTo(minio);
        return mediaFilesService.upLoadChunk(fileMd5, chunk, minio.getAbsolutePath());
    }

    //合并分块
    @PostMapping("/upload/mergechunks")
    public RestResponse<Boolean> mergeChunks(String fileMd5, String fileName, int chunkTotal){
        //获取机构id，还没实现，先瞎写一个
        Long comId = 1232141425L;
        MediaParamsDto mediaParamsDto = new MediaParamsDto();
        mediaParamsDto.setFilename(fileName);
        mediaParamsDto.setTags("视频文件");
        mediaParamsDto.setFileType("001002");
        mediaFilesService.emergeChunks(comId, fileMd5, chunkTotal, mediaParamsDto);

        return RestResponse.success(true);
    }

    //查询媒资文件,分页
    @PostMapping("/files")
    public PageResult<MediaFiles> getFilesOfVideo(PageParams pageParams,  @RequestBody(required = false) MediaConditionDto mediaConditionDto){
        log.info("分页媒资文件查询！！！");
        if(pageParams.getPageNo() != null && pageParams.getPageSize() != null) {
            Long pageSize = pageParams.getPageSize();
            Long pageNo = pageParams.getPageNo();
            Page<MediaFiles> mediaFilesPage = new Page<>(pageNo, pageSize);
            LambdaQueryWrapper<MediaFiles> mediaFilesLambdaQueryWrapper = new LambdaQueryWrapper<>();
            if(null != mediaConditionDto){
                mediaFilesLambdaQueryWrapper.like(null != mediaConditionDto.getFilename(), MediaFiles::getFilename, mediaConditionDto.getFilename());
                mediaFilesLambdaQueryWrapper.like(null != mediaConditionDto.getFileType(), MediaFiles::getFileType, mediaConditionDto.getFileType());
            }
            Page<MediaFiles> page = mediaFilesService.page(mediaFilesPage, mediaFilesLambdaQueryWrapper);
            return new PageResult<>(
                    page.getRecords(), page.getTotal(), pageNo, pageSize
            );
        }
        LambdaQueryWrapper<MediaFiles> mediaFilesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(null != mediaConditionDto){
            mediaFilesLambdaQueryWrapper.like(null != mediaConditionDto.getFilename(), MediaFiles::getFilename, mediaConditionDto.getFilename());
            mediaFilesLambdaQueryWrapper.like(null != mediaConditionDto.getType(), MediaFiles::getFileType, mediaConditionDto.getType());
            mediaFilesLambdaQueryWrapper.like(null != mediaConditionDto.getAuditStatus(), MediaFiles::getAuditStatus, mediaConditionDto.getAuditStatus());
        }
        List<MediaFiles> list = mediaFilesService.list(mediaFilesLambdaQueryWrapper);
        return new PageResult<MediaFiles>(
                list, 1L, 1L, 1L
        );

    }
}
